#include <QtGui>
#include <QTranslator>
#include <QtCore/QTextCodec>
#include "gv_setupdatedialog.h"
#include "gv_home.h"
#include "gv_setuptimezonedialog.h"
#include "gv_setting.h"

extern GV_Setting *gvsetting;
extern QDialog  *dialog;
GV_SetupDateDialog::GV_SetupDateDialog(QWidget *parent)
       : QDialog(parent)
{
    setupUi(this);

    QTranslator translator;
    translator.load(gvsetting->GV_GetLanguage(),".");
    qApp->installTranslator(&translator);
    reloadtext();

    TimeHintsLabel->setText((QDateTime::currentDateTime().toString("hh:mm:ss")));
    DateHintsLabel->setText((QDateTime::currentDateTime().toString("yyyy-MM-dd")));

    DateFrame->show();
    DisplaymodeFrame->hide();
    datestatus = DATESTATUSYEAR;
    mydisplaymode = DISPLAYMODEYDM;
    myyear=2010;
    mymonth=6;
    myday = 15;
    DateValueLabel->setText(QString::number(myyear));

    timer = new QTimer;
    connect(timer,SIGNAL(timeout()),this,SLOT(GV_chTime()));
    connect(BackButton,SIGNAL(clicked()),this,SLOT(cancel()));
    connect(CancelButton,SIGNAL(clicked()),this,SLOT(cancel()));
    connect(AcceptButton,SIGNAL(clicked()),this,SLOT(accept()));

    connect(DateSelectButton,SIGNAL(clicked()),this,SLOT(dateselect()));
    connect(DisplaymodeSelectButton,SIGNAL(clicked()),this,SLOT(displaymodeselect()));

    connect(YearButton,SIGNAL(clicked()),this,SLOT(setupyear()));
    connect(MonthButton,SIGNAL(clicked()),this,SLOT(setupmonth()));
    connect(DayButton,SIGNAL(clicked()),this,SLOT(setupday()));

    connect(DecreaseButton,SIGNAL(clicked()),this,SLOT(decreasevalue()));
    connect(IncreaseButton,SIGNAL(clicked()),this,SLOT(increasevalue()));

    connect(DMYRadio,SIGNAL(clicked()),this,SLOT(checkDMY()));
    connect(MDYRadio,SIGNAL(clicked()),this,SLOT(checkMDY()));
    connect(YMDRadio,SIGNAL(clicked()),this,SLOT(checkYMD()));

    timer->start(1000);
}
void GV_SetupDateDialog::reloadtext()
{
    //NameHintsLabel->setText(tr("Zones"));
}
void GV_SetupDateDialog::GV_chTime()
{
    TimeHintsLabel->setText((QDateTime::currentDateTime().toString("hh:mm:ss")));
    DateHintsLabel->setText((QDateTime::currentDateTime().toString("yyyy-MM-dd")));
}

void GV_SetupDateDialog::accept()
{
    QDialog *dialog1 = new GV_SetupTimezoneDialog;
    dialog1->show();
    delete dialog;
    dialog = dialog1;
}

void GV_SetupDateDialog::cancel()
{
    QDialog *dialog1 = new GV_SetupTimezoneDialog;
    dialog1->show();
    delete dialog;
    dialog = dialog1;
}

void GV_SetupDateDialog::dateselect()
{
    DisplaymodeFrame->hide();
    DateFrame->show();
}
void GV_SetupDateDialog::displaymodeselect()
{
    DisplaymodeFrame->show();
    DateFrame->hide();
    DMYRadio->setChecked(true);
}
void GV_SetupDateDialog:: setupyear()
{
    //myyear = 2010;
    datestatus = DATESTATUSYEAR;
    DateValueLabel->setText(QString::number(myyear));
}
void GV_SetupDateDialog:: setupmonth()
{
    //mymonth =6;
    datestatus = DATESTATUSMONTH;
    DateValueLabel->setText(QString::number(mymonth));
}
void GV_SetupDateDialog:: setupday()
{
    //myday = 15;
    datestatus = DATESTATUSDAY;
    DateValueLabel->setText(QString::number(myday));
}

void GV_SetupDateDialog:: decreasevalue()
{
    int maxday = 0;
    switch(datestatus){
    case DATESTATUSYEAR:
        if(myyear==2000){
            myyear = 2050;
        }else{
            myyear--;
        }
        DateValueLabel->setText(QString::number(myyear));
        break;
    case DATESTATUSMONTH:
        if(mymonth==1){
            mymonth = 12;
        }else{
            mymonth--;
        }
        DateValueLabel->setText(QString::number(mymonth));
        break;
    case DATESTATUSDAY:

        switch(mymonth)
        {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            maxday = 31;
            break;
        case 2:
            if(myyear%4==0){
                maxday = 29;
                if(myyear%100==0){
                    maxday = 28;
                }
            }else{
                maxday = 28;
            }
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            maxday = 30;
            break;
        default:
            break;
        }
        if(myday==1){
            myday = maxday;
        }else{
            myday--;
        }
        DateValueLabel->setText(QString::number(myday));
        break;
    default:
        break;
    }
}

void GV_SetupDateDialog:: increasevalue()
{
    int maxday = 0;
    switch(datestatus){
    case DATESTATUSYEAR:
        if(myyear==2050){
            myyear = 2000;
        }else{
            myyear++;
        }
        DateValueLabel->setText(QString::number(myyear));
        break;
    case DATESTATUSMONTH:
        //day changed? should fixed!
        if(mymonth==12){
            mymonth = 1;
        }else{
            mymonth++;
        }
        DateValueLabel->setText(QString::number(mymonth));
        break;
    case DATESTATUSDAY:

        switch(mymonth){
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            maxday = 31;
            break;
        case 2:
            if(myyear%4==0){
                maxday = 29;
                if(myyear%100==0){
                    maxday = 28;
                }
            }else{
                maxday = 28;
            }
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            maxday = 30;
            break;
        }
        if(myday==maxday){
            myday = 1;
        }else{
            myday++;
        }
        DateValueLabel->setText(QString::number(myday));
        break;
    default:
        break;
    }
}

void GV_SetupDateDialog:: checkDMY()
{
    mydisplaymode = DISPLAYMODEDMY;
}
void GV_SetupDateDialog:: checkMDY()
{
    mydisplaymode = DISPLAYMODEMDY;
}
void GV_SetupDateDialog:: checkYDM()
{
    mydisplaymode = DISPLAYMODEYDM;
}

